/**
 * 
 */
package offer.algorithm;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author zhang 正整数数组，排列成最小的数
 *
 */
public class ArrayRangeMin33 {

	public static String getMinNum(int[] array) {
		if (array == null || array.length <= 0) {
			return null;
		}
		String[] strings = new String[array.length];
		for (int i = 0; i < strings.length; i++) {
			strings[i] = array[i] + "";
		}
		Comparator<String> compartor = new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				boolean status = false;
				int i;
				for (i = 0; i < o1.length() && i < o2.length(); i++) {
					if (o2.charAt(i) > o1.charAt(i)) {
						status = true;
						break;
					}
				}
				if (!status && i == o1.length()) {
					for (int j = i; j < o2.length(); j++) {
						if (o2.charAt(j) > o1.charAt(i - 1)) {
							status = true;
							break;
						}
					}
				} else if (!status && i == o2.length()) {
					for (int j = i; j < o1.length(); j++) {
						if (o2.charAt(i - 1) > o1.charAt(j)) {
							status = true;
							break;
						}
					}
				}
				return status ? -1 : 1;
			}
		};
		Arrays.sort(strings, compartor);
		StringBuilder sBuilder = new StringBuilder();
		for (int i = 0; i < strings.length; i++) {
			sBuilder.append(strings[i]);
		}
		return sBuilder.toString();
	}

	public static void main(String[] args) {
		int[] array = { 3334, 3, 3333332 };
		String minNum = getMinNum(array);
		System.out.println(minNum);
	}

}
